package dy.app.component;

import com.alibaba.fastjson2.JSONObject;
import dy.app.dto.InterceptorDto;
import dy.app.dto.SysCacheUserDto;
import dy.app.enums.CacheKeyEnums;
import dy.app.interceptor.LoginInterceptor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 权限组件
 * Author: cKnight
 * Date: 2024/11
 */
@Component
public class AuthComponent {

    @Resource
    private RedisCacheComponent redisCacheComponent;

    /**
     * 获取后管用户
     * @return
     */
    public SysCacheUserDto getAdminUserInfo() {
        InterceptorDto interceptorDto = LoginInterceptor.loginThreadlocal.get();
        if (Objects.isNull(interceptorDto)){
            //不是后管用户
            return null;
        }
        //从 threadLocal中获取token
        String key = String.format(CacheKeyEnums.ADMIN_LOGIN_KEY.getKey(), interceptorDto.getAdminToken());
        String cache = redisCacheComponent.get(key);
        if (StringUtils.isBlank(cache)){
            return null;
        }
        return JSONObject.parseObject(cache,SysCacheUserDto.class);
    }
}
